#include <iostream>
using namespace std;
#include <vector>

class Solution
{
    vector<string> ret;
    string path;
    int left, right;
public:
    vector<string> generateParenthesis(int n)
    {
        dfs(n);
        return ret;
    }

    void dfs(int n)
    {
        if (right == n)
        {
            ret.push_back(path);
            return;
        }

        if (left < n)
        {
            path += '('; left++;
            dfs(n);
            path.pop_back(); --left;
        }

        if (right < left)
        {
            path += ')'; right++;
            dfs(n);
            path.pop_back(); --right;
        }
    }
};

int main()
{
    Solution s;
    auto ret = s.generateParenthesis(3);
    return 0;
}